package com.zhong.thesimpleconnection.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zhong.thesimpleconnection.R;

import java.util.List;

/**
 * Created by 俊峰 on 2017/5/26.
 */

public class MyActivityListAdapter extends BaseAdapter {

    private List<String> mStringList;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private int mHeight;

    public MyActivityListAdapter(List<String> m, Context context, int height){
        mStringList = m;
        mContext = context;
        mHeight = height;
        mLayoutInflater = LayoutInflater.from(this.mContext);
    }

    @Override
    public int getCount() {
        return mStringList.size();
    }

    @Override
    public Object getItem(int position) {
        return mStringList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret;
        if (convertView == null){
            ret = mLayoutInflater.inflate(R.layout.activity_find_list_item, parent, false);
        } else {
            ret = convertView;
        }
        ViewHolder viewHolder = (ViewHolder) ret.getTag();
        if (viewHolder == null){
            viewHolder = new ViewHolder(ret);
            ret.setTag(viewHolder);
        }
        onBindView(viewHolder, mStringList.get(position), mStringList.size());
        return ret;
    }

    private void onBindView(ViewHolder holder, String listItem, int count) {
        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.mView.getLayoutParams();
        params.height = mHeight / count;
        holder.mView.setLayoutParams(params);
        holder.mTextView.setText(listItem);
    }

    private static class ViewHolder{
        private TextView mTextView;
        private View mView;

        public ViewHolder(View theAll){
            mTextView = (TextView) theAll.findViewById(R.id.fragment_find_list_item_text);
            mView = theAll.findViewById(R.id.fragment_find_list_item_some);
        }
    }
}
